{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo of the PlateauFinder transformer\n",
    "\n",
    "What does the PlateauFinder do?\n",
    "\n",
    "* It searches for time series segments of a given minimum length  with a constant given value (i.e. plateaus) and returns their starting points (on the time series index) and lengths,\n",
    "* The value to search for can also be set to `np.nan` or `np.inf` to find missing values,\n",
    "* The minimum length of segments to consider can also be specified; if set to 1, returns as starting points all locations of the given value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-12-19T14:32:43.679446Z",
     "iopub.status.busy": "2020-12-19T14:32:43.678702Z",
     "iopub.status.idle": "2020-12-19T14:32:44.286616Z",
     "shell.execute_reply": "2020-12-19T14:32:44.287292Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sktime.transformations.panel.summarize import PlateauFinder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-12-19T14:32:44.302616Z",
     "iopub.status.busy": "2020-12-19T14:32:44.298946Z",
     "iopub.status.idle": "2020-12-19T14:32:44.305294Z",
     "shell.execute_reply": "2020-12-19T14:32:44.305873Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0    NaN\n",
       "1    NaN\n",
       "2    3.0\n",
       "3    3.0\n",
       "4    NaN\n",
       "5...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0    0.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    NaN\n",
       "4    NaN\n",
       "5...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0    2.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    NaN\n",
       "4    2.0\n",
       "5...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0    1.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    3.0\n",
       "4    NaN\n",
       "5...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   0\n",
       "0  0    NaN\n",
       "1    NaN\n",
       "2    3.0\n",
       "3    3.0\n",
       "4    NaN\n",
       "5...\n",
       "1  0    0.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    NaN\n",
       "4    NaN\n",
       "5...\n",
       "2  0    2.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    NaN\n",
       "4    2.0\n",
       "5...\n",
       "3  0    1.0\n",
       "1    NaN\n",
       "2    NaN\n",
       "3    3.0\n",
       "4    NaN\n",
       "5..."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# generate toy data\n",
    "X = pd.DataFrame(\n",
    "    pd.Series(\n",
    "        [\n",
    "            pd.Series([np.nan, np.nan, 3, 3, np.nan, 2, 2, 3]),\n",
    "            pd.Series([0, np.nan, np.nan, np.nan, np.nan, np.nan, 2, np.nan]),\n",
    "            pd.Series([2, np.nan, np.nan, np.nan, 2, np.nan, 3, 1]),\n",
    "            pd.Series([1, np.nan, np.nan, 3, np.nan, np.nan, 2, 0]),\n",
    "        ]\n",
    "    )\n",
    ")\n",
    "X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-12-19T14:32:44.315008Z",
     "iopub.status.busy": "2020-12-19T14:32:44.314448Z",
     "iopub.status.idle": "2020-12-19T14:32:44.316887Z",
     "shell.execute_reply": "2020-12-19T14:32:44.317666Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0_nan_starts</th>\n",
       "      <th>0_nan_lengths</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[0]</td>\n",
       "      <td>[2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[1]</td>\n",
       "      <td>[5]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[1]</td>\n",
       "      <td>[3]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[1, 4]</td>\n",
       "      <td>[2, 2]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  0_nan_starts 0_nan_lengths\n",
       "0          [0]           [2]\n",
       "1          [1]           [5]\n",
       "2          [1]           [3]\n",
       "3       [1, 4]        [2, 2]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  find plateaus\n",
    "t = PlateauFinder()\n",
    "Xt = t.fit_transform(X)\n",
    "Xt"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
